package com.glory.wxelectricitypda.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.BaseDialogActivity;

import com.glory.wxelectricitypda.bean.FTPittedSubmitBean;
import com.glory.wxelectricitypda.bean.PittedBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FTPittedActivity extends BaseDialogActivity {

    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.subID)
    FreeUI_ClearEditText subID;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.mainqty)
    TextView mainqty;
    @BindView(R.id.state)
    TextView state;
    @BindView(R.id.stepname)
    TextView stepname;
    @BindView(R.id.customerOrder)
    TextView customerOrder;
    @BindView(R.id.partname)
    TextView partname;
    @BindView(R.id.flStepName)
    FrameLayout flStepName;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.ivEqpId)
    ImageView ivEqpId;

    private List<String> eqpIdList = new ArrayList<>();
    private FreeUI_GeneralPop mEqpIdFalgPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ftpitted;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("FT进站");
        freeUI_titleView.setRightListener("下一步", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (etLotId.getText().toString().equals("")) {
                    showToast("请扫描批次ID", false);
                    return;
                }
                if (tvEqpId.getText().toString().equals("")) {
                    showToast("请选择主设备ID", false);
                    return;
                }
                if (subID.getText().toString().equals("")) {
                    showToast("请扫描子设备", false);
                    return;
                }

                newSubmit();
//                submit();
//                startActivity(new Intent(FTPittedActivity.this,FTChangeMantissaActivity.class));
            }
        });
    }

    @Override
    protected void initData() {

        initEdit(etLotId,s->{
            queryPitted();
        });

        flStepName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mEqpIdFalgPop != null){
                    if (mEqpIdFalgPop.isShowing()){
                        mEqpIdFalgPop.dismiss();
                    }
                }else {
                    mEqpIdFalgPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flStepName.getWidth(), eqpIdList, ivEqpId, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {

                            tvEqpId.setText(s);
                        }
                    });
                }

                FreeApi_PopUtils.setArrowShow(ivEqpId);
                mEqpIdFalgPop.showAsDropDown(flStepName,0,0);
            }
        });
    }

    public void queryPitted(){

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", etLotId.getText().toString());
            jsonObject1.put("ACTIONTYPE", "GETLOTINFO");
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITLOTQUERY");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<PittedBean>>(
                new TypeToken<ResponseBean<PittedBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<PittedBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    if (!response.body().getBody().getTRACKSHOWMSG().isEmpty()){
                        FreeApi_DialogUtils.getConfirmDialog(FTPittedActivity.this, "确定", response.body().getBody().getTRACKSHOWMSG(), new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                            }
                        }).show(getSupportFragmentManager(),"DIALOG");
                    }
                    mainqty.setText(String.valueOf(response.body().getBody().getLOT().getMAINQTY()));
                    state.setText(response.body().getBody().getLOT().getSTATE());
                    stepname.setText(response.body().getBody().getLOT().getSTEPNAME());
                    customerOrder.setText(response.body().getBody().getWORKORDER().getCUSTOMER_ORDER());
                    partname.setText(response.body().getBody().getLOT().getPARTNAME());
                    for (PittedBean.EQUIPMENTLISTDTO bean:response.body().getBody().getEQUIPMENTLIST()) {
                        eqpIdList.add(bean.getEQUIPMENTID());
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<PittedBean>> response) {
                super.onError(response);
                loadError(response.getException(), "sendRequest");
            }
        });

    }

    private void submit(){

        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONArray lotList = new JSONArray();
        JSONObject lotObject = new JSONObject();
        try {
            jsonObject1.put("ACTIONTYPE", "FTTrackIn");
            jsonObject1.put("EQUIPMENTID", tvEqpId.getText().toString());
            jsonArray.put(subID.getText().toString());
            jsonObject1.put("SUBEQUIPMENTIDLIST", jsonArray);
            jsonObject1.put("ISCHECKSTEPAUTHORITY", true);
            lotObject.put("LOTID",etLotId.getText().toString());
            lotList.put(lotObject);
            jsonObject1.put("LOTLIST", lotList);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("lotFtTrackin","lotFtTrackin",jsonObject1,this, new FreeUI_DialogEntityCallBack<ResponseBean<FTPittedSubmitBean>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTPittedSubmitBean>> response) {

                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())){
                    showToast("进站成功", true);
                    if (!response.body().getBody().getTRACKSHOWMSG().isEmpty()){
                        FreeApi_DialogUtils.getConfirmAndCancelDialog(FTPittedActivity.this, response.body().getBody().getTRACKSHOWMSG(), new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {

                                requestMessage(response.body().getBody());
                            }
                        }, () -> {}).show(getSupportFragmentManager(),"DELETE_DIALOG");
                    }else {

                        requestMessage(response.body().getBody());
                    }
                }

            }

            @Override
            public void onError(Response<ResponseBean<FTPittedSubmitBean>> response) {
                super.onError(response);
                loadError(response.getException(), "lotFtTrackin");
            }
        });


    }


    private void newSubmit(){

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONArray lotList = new JSONArray();
        JSONObject lotObject = new JSONObject();
        try {
            jsonObject1.put("ACTIONTYPE", "FTMergeEndLot");
            jsonObject1.put("EQUIPMENTID", tvEqpId.getText().toString());
            jsonArray.put(subID.getText().toString());
            jsonObject1.put("SUBEQUIPMENTIDLIST", jsonArray);
            jsonObject1.put("ISCHECKSTEPAUTHORITY", true);
            lotObject.put("LOTID",etLotId.getText().toString());
            lotList.put(lotObject);
            jsonObject1.put("LOTLIST", lotList);
            jsonObject.put("Data", jsonObject1);
            jsonObject.put("MessageName", "EUIT.LOTFTTRACKIN");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<FTPittedSubmitBean>>(
                new TypeToken<ResponseBean<FTPittedSubmitBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTPittedSubmitBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    if (!response.body().getBody().getTRACKSHOWMSG().isEmpty()){
                        FreeApi_DialogUtils.getConfirmAndCancelDialog(FTPittedActivity.this, response.body().getBody().getTRACKSHOWMSG(), new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {

                                requestMessage(response.body().getBody());
                            }
                        }, () -> {}).show(getSupportFragmentManager(),"DELETE_DIALOG");
                    }else {

                        requestMessage(response.body().getBody());
                    }

                }
            }

            @Override
            public void onError(Response<ResponseBean<FTPittedSubmitBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTFTTRACKIN");
            }
        });


    }

    private void requestMessage(FTPittedSubmitBean bean){
        if (bean.getCANFLIPNEXTPAGE()){
            Intent intent = new Intent(FTPittedActivity.this,FTChangeMantissaActivity.class);
            intent.putParcelableArrayListExtra("FTNEEDMERGELOTLIST",new ArrayList<>(bean.getLOTLIST()));
            intent.putExtra("EQUIPMENTID",tvEqpId.getText().toString());
            intent.putExtra("SUBEQUIPMENTIDLIST",subID.getText().toString());
            intent.putExtra("LOTID",etLotId.getText().toString());
            startActivityForResult(intent,0xf1);
        }else {
//            showToast("规格不符，无法提交！",false);
            submitEND(bean);
        }
    }


    private void submitEND(FTPittedSubmitBean bean){

        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONArray lotList = new JSONArray();
        JSONObject lotObject = new JSONObject();
        JSONArray lotList01 = new JSONArray();
        try {
            jsonObject1.put("ACTIONTYPE", "FTTrackIn");
            jsonObject1.put("EQUIPMENTID", tvEqpId.getText().toString());
            jsonArray.put(subID.getText().toString());
            jsonObject1.put("SUBEQUIPMENTIDLIST", jsonArray);
            jsonObject1.put("ISCHECKSTEPAUTHORITY", true);
            lotObject.put("LOTID",etLotId.getText().toString());
            lotList.put(lotObject);
            jsonObject1.put("LOTLIST", lotList);
//            ArrayList<Integer> lotList1 = bean.getLotList();
            for (int i = 0;i<bean.getLOTLIST().size();i++){
                JSONObject object = new JSONObject();
                object.put("LOTID",bean.getLOTLIST().get(i).getLOTID());
                lotList01.put(object);
            }
            jsonObject1.put("FTNEEDMERGELOTLIST", lotList01);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("lotFtTrackin","lotFtTrackin",jsonObject1,this, new FreeUI_DialogEntityCallBack<ResponseBean<FTPittedSubmitBean>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTPittedSubmitBean>> response) {

                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())){
                    showToast("进站成功", true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
//                            Intent intent = new Intent();
//                            setResult(Activity.RESULT_OK,intent);
                            finish();
                        }
                    }, 2000);
                }

            }

            @Override
            public void onError(Response<ResponseBean<FTPittedSubmitBean>> response) {
                super.onError(response);
                loadError(response.getException(), "lotFtTrackin");
            }
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0xf1 && resultCode == Activity.RESULT_OK){
            finish();
        }
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }
}